﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace SZSLSystem.DAL
{
	/// <summary>
	///数据访问类 Buyer (create by HC 2013/1/14 22:28:41)
	/// </summary>
	public class Buyer
	{
		public Buyer(){}
		public bool Exists(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(OID) from Buyer");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.Exists(strSql.ToString(), parameters);
		}

		public int Delete(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from Buyer");
			strSql.Append(" where OID=@OID  ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public int Add(SZSLSystem.Model.Buyer model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Buyer( ");
			strSql.Append("[OID],[BuyerName],[CredentialsType],[CredentialsNumber],[Sex],[Birthday],[Mobile],[Telphone],[PostalAddress],[PostalCode])");
			strSql.Append("values (@OID,@BuyerName,@CredentialsType,@CredentialsNumber,@Sex,@Birthday,@Mobile,@Telphone,@PostalAddress,@PostalCode)");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@BuyerName",SqlDbType.VarChar,50),
					new SqlParameter ("@CredentialsType",SqlDbType.VarChar,50),
					new SqlParameter ("@CredentialsNumber",SqlDbType.VarChar,50),
					new SqlParameter ("@Sex",SqlDbType.VarChar,50),
					new SqlParameter ("@Birthday",SqlDbType.VarChar,50),
					new SqlParameter ("@Mobile",SqlDbType.VarChar,50),
					new SqlParameter ("@Telphone",SqlDbType.VarChar,50),
					new SqlParameter ("@PostalAddress",SqlDbType.VarChar,50),
					new SqlParameter ("@PostalCode",SqlDbType.VarChar,50)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.BuyerName;
			parameters[2].Value = model.CredentialsType;
			parameters[3].Value = model.CredentialsNumber;
			parameters[4].Value = model.Sex;
			parameters[5].Value = model.Birthday;
			parameters[6].Value = model.Mobile;
			parameters[7].Value = model.Telphone;
			parameters[8].Value = model.PostalAddress;
			parameters[9].Value = model.PostalCode;
			object obj = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
			if (obj == null)
			{
				return 1;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		public  SZSLSystem.Model.Buyer GetModel(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select top 1 [OID],[BuyerName],[CredentialsType],[CredentialsNumber],[Sex],[Birthday],[Mobile],[Telphone],[PostalAddress],[PostalCode] from Buyer");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			SZSLSystem.Model.Buyer model=new SZSLSystem.Model.Buyer();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				DataRow row = ds.Tables[0].Rows[0];
						model.OID=row["OID"].ToString();
						model.BuyerName=row["BuyerName"].ToString();
						model.CredentialsType=row["CredentialsType"].ToString();
						model.CredentialsNumber=row["CredentialsNumber"].ToString();
						model.Sex=row["Sex"].ToString();
						model.Birthday=row["Birthday"].ToString();
						model.Mobile=row["Mobile"].ToString();
						model.Telphone=row["Telphone"].ToString();
						model.PostalAddress=row["PostalAddress"].ToString();
						model.PostalCode=row["PostalCode"].ToString();

				return model;
			}
			else
			{
				return null;
			}
		}

		public int Update(SZSLSystem.Model.Buyer model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Buyer set ");
			strSql.Append("[OID]=@OID,");
			strSql.Append("[BuyerName]=@BuyerName,");
			strSql.Append("[CredentialsType]=@CredentialsType,");
			strSql.Append("[CredentialsNumber]=@CredentialsNumber,");
			strSql.Append("[Sex]=@Sex,");
			strSql.Append("[Birthday]=@Birthday,");
			strSql.Append("[Mobile]=@Mobile,");
			strSql.Append("[Telphone]=@Telphone,");
			strSql.Append("[PostalAddress]=@PostalAddress,");
			strSql.Append("[PostalCode]=@PostalCode");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@BuyerName",SqlDbType.VarChar,50),
					new SqlParameter ("@CredentialsType",SqlDbType.VarChar,50),
					new SqlParameter ("@CredentialsNumber",SqlDbType.VarChar,50),
					new SqlParameter ("@Sex",SqlDbType.VarChar,50),
					new SqlParameter ("@Birthday",SqlDbType.VarChar,50),
					new SqlParameter ("@Mobile",SqlDbType.VarChar,50),
					new SqlParameter ("@Telphone",SqlDbType.VarChar,50),
					new SqlParameter ("@PostalAddress",SqlDbType.VarChar,50),
					new SqlParameter ("@PostalCode",SqlDbType.VarChar,50)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.BuyerName;
			parameters[2].Value = model.CredentialsType;
			parameters[3].Value = model.CredentialsNumber;
			parameters[4].Value = model.Sex;
			parameters[5].Value = model.Birthday;
			parameters[6].Value = model.Mobile;
			parameters[7].Value = model.Telphone;
			parameters[8].Value = model.PostalAddress;
			parameters[9].Value = model.PostalCode;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select [OID],[BuyerName],[CredentialsType],[CredentialsNumber],[Sex],[Birthday],[Mobile],[Telphone],[PostalAddress],[PostalCode]");
			strSql.Append(" FROM Buyer ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(int top,string strWhere,string fieldOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select");
			if(top>0)
			{
				strSql.Append(" top "+top.ToString());
			}
			strSql.Append(" [OID],[BuyerName],[CredentialsType],[CredentialsNumber],[Sex],[Birthday],[Mobile],[Telphone],[PostalAddress],[PostalCode]");
			strSql.Append(" FROM Buyer ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
				strSql.Append(" order by "+fieldOrder);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(string showFields,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+showFields);
			strSql.Append(" FROM Buyer ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public object GetSingle(string fieldName,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+fieldName);
			strSql.Append(" FROM Buyer ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.GetSingle(strSql.ToString());
		}

	}
}